package com.weiyun.shortlink.admin.config;

import com.weiyun.shortlink.admin.biz.user.UserFlowRiskControlFilter;
import com.weiyun.shortlink.admin.biz.user.UserTransmitFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/***
 *   @Author:weiyun
 *
 */
@Configuration
public class UserConfiguration {
    /**
     * 用户信息传递过滤器
     */
    @Bean
    public FilterRegistrationBean<UserTransmitFilter> globalUserTransmitFilter() {
        FilterRegistrationBean<UserTransmitFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new UserTransmitFilter());
        registration.addUrlPatterns("/*");
//        registration.addInitParameter("Login","/api/shortLink/v1/user/login");
        registration.setOrder(0);
        return registration;
    }

    @Bean
    @ConditionalOnProperty(name = "short-link.flow-limit.enable",havingValue = "true")
    public FilterRegistrationBean<UserFlowRiskControlFilter> globalUserFlowRiskControllerFilter(
            StringRedisTemplate stringRedisTemplate,
            UserFlowRiskControllerConfiguration userFlowRiskControllerConfiguration
    ) {
        FilterRegistrationBean<UserFlowRiskControlFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new UserFlowRiskControlFilter(stringRedisTemplate, userFlowRiskControllerConfiguration));
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(10);
        return registrationBean;
    }
}
